package com.example.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Book;
import com.example.entity.Params;
import com.example.exception.CustomException;
import com.example.mapper.BookMapper;
import com.example.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.summingInt;

/**
 * @Author: PengFei.He
 * @Date: 2024/11/6 19:52
 * @Function:
 */

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Autowired
    private BookMapper bookMapper;


    @Override
    public List<Book> findBySearch(Params params) {
        return bookMapper.findBySearch(params);
    }

    // 新增和编辑公用一个接口
    @Override
    public void add(Book book) {
        if (book.getId() == null){
            bookMapper.insert(book);
        }
        bookMapper.updateById(book);
    }

    @Override
    public List<Book> findAll() {
        return bookMapper.findAll();
    }

    @Override
    public List<Map<String, Object>> bie() {
        List<Book> allBooks = this.findAll();
        List<Map<String, Object>> list = new ArrayList<>();
        if (CollectionUtils.isEmpty(allBooks)){
            throw new CustomException("没有图书信息");
        }
//        Map<String, List<Book>> map = allBooks.stream().collect(groupingBy(Book::getTypeName));
//        for (Map.Entry<String, List<Book>> entry : map.entrySet()) {
//            HashMap<String, String> hashMap = new HashMap<>();
//            hashMap.put("name", entry.getKey());
//            hashMap.put("value", String.valueOf(entry.getValue().size()));
//            list.add(hashMap);
//        }

        /*
            根据类型名称分组，分组内容是类型名称的个数
            这个map里存放的是{技术类=2, 情感类=2}
            可以理解成一个Vo对象，属性是 key1 ,key2
            Vo vo = new Vo();
            vo.setKey1(2);
            vo.setKey2(2);
         */
        Map<String, Long> map = allBooks.stream().filter(book -> ObjectUtil.isNotNull(book.getTypeName()))
                .collect(groupingBy(Book::getTypeName, Collectors.counting()));
        for (Map.Entry<String, Long> entry : map.entrySet()) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("name", entry.getKey());
            hashMap.put("value",entry.getValue());
            list.add(hashMap);
        }
        return list;
    }

    @Override
    public Map<Object, Object> bar() {
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();

        List<Book> allBooks = this.findAll();
        Map<String, Long> map = allBooks.stream().filter(book -> ObjectUtil.isNotNull(book.getTypeName()))
                .collect(groupingBy(Book::getTypeName, Collectors.counting()));
        for (Map.Entry<String, Long> entry : map.entrySet()) {
            xAxis.add(entry.getKey());
            yAxis.add(String.valueOf(entry.getValue()));
        }

        Map<Object, Object> result = new HashMap<>();
        result.put("xAxis", xAxis);
        result.put("yAxis", yAxis);
        return result;
    }


}
